﻿Ext.namespace('admin.menu');

admin.menu.Main = function(config) {
	Ext.apply(this, config);
	this.initUIComponents();
	admin.menu.Main.superclass.constructor.call(this);
};

Ext.extend(admin.menu.Main, Ext.Panel, {
	/**
	 * 构造函数
	 */
	initUIComponents : function() {

		this.recordAll = new Ext.data.Record({
			NAME : '全部',
			ID : '-1'
		});

		this.westPanel = this.tree = new Ext.tree.TreePanel({
			region : "west",
			title : '菜单',
			width : 245,
			border : false,
			split : true,
			collapsible : true,
			loader : new Ext.tree.TreeLoader({
				baseParams : {
					nodeId : '-1'
				},
				dataUrl : ExtJ
						.getActionPath('/cud/queryMenuListWithOutCheckBox.do')
			}),
			root : new Ext.tree.AsyncTreeNode({
				id : '-1',
				draggable : false,
				expanded : false,
				text : ''
			}),
			tbar : [ {
				id : 'add',
				text : '新增菜单',
				iconCls : 'add',
				menu : [ {
					id : 'add_tj',
					text : '新增同级',
					iconCls : 'add',
					handler : this.onAddTj.createDelegate(this)
				}, {
					id : 'add_xj',
					text : '新增下级',
					iconCls : 'add',
					handler : this.onAddXj.createDelegate(this)
				} ]
			}, {
				id : 'updateMenu',
				text : '修改菜单',
				iconCls : 'update',
				handler : this.onUpdateMenu.createDelegate(this)
			}, {
				id : 'deleteMenu',
				text : '删除菜单',
				iconCls : 'delete',
				handler : this.onDeleteMenu.createDelegate(this)
			} ],
			autoScroll : true,
			animate : false,
			border : false,
			enableDD : false,
			containerScroll : true,
			singleExpand : false,
			draggable : false,
			rootVisible : false,
			listeners : {
				'render' : function() {
				}.createDelegate(this),
				'beforeload' : function(node, e) {
					this.tree.getLoader().baseParams.nodeId = node.id;
				}.createDelegate(this),
				'click' : this.onTreeNodeClick.createDelegate(this)
			}
		});
		/*this.menuRoleStore = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : ExtJ.getActionPath("role/queryRoleListAll.do")
			}),
			reader : new Ext.data.JsonReader({
				root : 'data',
				totalProperty : 'total',
				id : 'ID'
			}, [ {
				name : 'NAME',
				mapping : 'NAME'
			}, {
				name : 'ID',
				mapping : 'ID'
			} ])
		});
		
		

		
		
		this.menuRoleStore.load({
			callback : function(store, records) {
				this.menuRoleStore.insert(0, this.recordAll);
			}.createDelegate(this)
		});
		
		
		var menuRoleCombo = new Ext.form.MultiComboBox({
			fieldLabel : '所属权限',
			name : 'MENU_ROLE',
			id : 'MENU_ROLE',
			anchor : '90%',
			store : this.menuRoleStore,
			valueField : 'ID',
			displayField : 'NAME',
			hiddenName : 'MENU_ROLE_VALUE',
			triggerAction : 'all',
			mode : 'local',
			editable : false,
			selectOnFocus : true,
			readOnly:true,
			//hidden:true,
			listeners : {
				'select' : function() {
					var shareRoleIds = Ext.getCmp('MENU_ROLE').getValue();
					var ids = shareRoleIds.split(",");
					for (var i = 0; i < ids.length; i++) {
						if ("-1" == ids[i]) {
							Ext.getCmp('MENU_ROLE').checkAll();
						}
					}
				}.createDelegate(this)
			}
		});*/
		
		var menuRoletree =  new Ext.tree.TreePanel({
			id : 'HANDLE_TREE',
			height : 100,
			rootVisible : false,
			animate : true,
			autoScroll : true,
			loader : new Ext.tree.TreeLoader({
				baseParams : {
					nodeId : '-1'
				},
				baseAttrs : {
						uiProvider : Ext.tree.TreeCheckNodeUI					
					},
				dataUrl : ExtJ.getActionPath('role/queryRoleListchecked.do')
			}),
			root : new Ext.tree.AsyncTreeNode({
				id : '-1',
				text : '根'
			}),
			listeners : {
				'beforeload' : function(node, e) {
					Ext.getCmp('HANDLE_TREE').getLoader().baseParams.nodeId = node.id;
				}.createDelegate(this)
				,'load' : function(node, e) {
					Ext.getCmp('COM_TREE_ID').setValue();
				}.createDelegate(this)
			}
		})
		
		this.centerPanel = this.formPanel = new Ext.form.FormPanel({
			layout : 'form',
			region : 'center',
			title : '详细信息',
			labelWidth : 125,
			labelAlign : 'right',
			autoHeight : true,
			border : false,
			frame : false,
			bodyStyle : 'padding:5px 5px 0',
			buttonAlign : 'center',
			defaultType : 'textfield',
			items : [ new Ext.form.Hidden({
				fieldLabel : '菜单ID',
				id : 'MENU_ID',
				name : 'MENU_ID'
			}), {
				fieldLabel : '菜单名称',
				name : 'MENU_NAME',
				id : 'MENU_NAME',
				anchor : '90%',
				readOnly : true,
				xtype : 'textfield'
			}, {
				fieldLabel : '菜单地址',
				name : 'MENU_URL',
				id : 'MENU_URL',
				anchor : '90%',
				readOnly : true,
				xtype : 'textfield'
			}/*, {
				fieldLabel : '排序',
				name : 'ORDER_NUM',
				id : 'ORDER_NUM',
				anchor : '90%',
				readOnly : true,
				xtype : 'textfield'
			}*/, {
				fieldLabel : '是否是叶子节点',
				name : 'MENU_IS_LEAF',
				id : 'MENU_IS_LEAF',
				anchor : '90%',
				readOnly : true,
				xtype : 'textfield'
			}, {
				fieldLabel : '所属权限',
				name : 'MENU_ROLE',
				id : 'MENU_ROLE',
				anchor : '90%',
				readOnly : true,
				xtype : 'textfield'
			},new Ext.ux.ComboBoxCheckTree({
						anchor : '90%',
						fieldLabel : '权限分配',
						name : 'COM_TREE_ID',
						id : 'COM_TREE_ID',
						tree : menuRoletree,
						selectValueModel : 'all',
						listeners : {
							'expand' : function() {
								Ext.getCmp('HANDLE_TREE').getRootNode().reload();
								Ext.getCmp('HANDLE_TREE').expandAll();
							}.createDelegate(this)
						}
					})
				],
			buttons : [ {
				text : '保存',
				handler : this.onSave.createDelegate(this)
			}, {
				text : '取消',
				handler : this.onCancel.createDelegate(this)
			} ]
		});

		
		Ext.apply(this, {
			closeAction : 'close',
			layout : 'border',
			border : false,
			items : [  this.westPanel,this.centerPanel ],
			listeners : {
				'render' : this.initData.createDelegate(this)
			}
		})
	},

	initData : function() {
	//	Ext.getCmp('COM_TREE_ID').getValue();
	},

	
	
	
	onTreeNodeClick : function(node, e) {
		this.formPanel.getForm().reset();
		var pageId = node.attributes.id;
		var pageName = node.attributes.text;
		var pageUrl = node.attributes.url;
		var pagepx = node.attributes.px;
		Ext.getCmp('MENU_ID').setValue(pageId);
		Ext.getCmp('MENU_NAME').setValue(pageName);
		Ext.getCmp('MENU_URL').setValue(pageUrl);
		Ext.getCmp('ORDER_NUM').setValue(pagepx);
		if (node.leaf) {
			Ext.getCmp('MENU_IS_LEAF').setValue("是");
		} else {
			Ext.getCmp('MENU_IS_LEAF').setValue("否");
		}
		Ext.getBody().mask("数据加载中....");
		Ext.Ajax.request({
			url : ExtJ.getActionPath("/cud/queryMenuRoleList.do"),
			params : {
				menuId : pageId
			},
			callback : function(options, suc, response) {
				if (suc) {
					var text = Ext.decode(response.responseText);
					var menuRoleArr = text.data;
					//console.log(menuRoleArr);
					var roleIds = "", roleNames = "";
					for (var i = 0; i < menuRoleArr.length; i++) {
						var roleId = menuRoleArr[i].ID;
						var roleName = menuRoleArr[i].NAME;
						if (0 != i) {
							roleIds += ",";
							roleNames += ",";
						}
						roleIds += roleId;
						roleNames += roleName;
					}
					Ext.getCmp('MENU_ROLE').setValue(roleNames);
					Ext.getCmp('HANDLE_TREE').getLoader().baseParams.menuId = pageId;
					//Ext.getCmp('COM_TREE_ID').fireEvent('expand');
					Ext.getBody().unmask(true);
				}
			}.createDelegate(this)
		});
	},
	onSave : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		if (!selectNode) {
			Ext.Msg.alert("提示", "请选择一个菜单节点!");
			return;
		} else {

			var menuId = Ext.getCmp('MENU_ID').getValue();
			var menuName = Ext.getCmp('MENU_NAME').getValue();
			var menuUrl = Ext.getCmp('MENU_URL').getValue();
			var menuRoleIds = Ext.getCmp('COM_TREE_ID').getValue();
			var menuRoleNames = Ext.getCmp('COM_TREE_ID').getRawValue();
			if ('' == menuId) {
				ExtJ.showMessage("对不起，您的操作不正确!");
				return;
			}
			if ('' == menuName) {
				ExtJ.showMessage("对不起，菜单名称不能为空!");
				return;
			}
			this.formPanel.getForm().submit({
				method : 'post',
				url : ExtJ.getActionPath("/cud/addMenuRoleRelatinShip.do"),
				params : {
					menuId : menuId,
					menuRoleIds : menuRoleIds,
					menuRoleNames : menuRoleNames
				},
				reset : false,
				success : function(option, action) {
					var text = Ext.decode(action.response.responseText);
					if (text.isAdd) {
						Ext.Msg.alert('提示', '保存成功!');
					} else {
						Ext.Msg.alert('提示', '保存失败!');
					}
					//this.formPanel.getForm().reset();
					//Ext.getCmp('COM_TREE_ID').clearValue();
					
				}.createDelegate(this)
			});
		}

	},

	onCancel : function() {
		this.formPanel.getForm().reset();
	},

	onAddTj : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示", "请选中一个菜单节点！");
			return;
		}
		var win = new admin.menu.MenuAdd({
			operType : 'tj',
			selectedTreeNode : selectNode,
			treepanel : treepanel
		});
		win.show();
	},

	onAddXj : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示", "请选中一个菜单节点！");
			return;
		}
		var win = new admin.menu.MenuAdd({
			operType : 'xj',
			selectedTreeNode : selectNode,
			treepanel : treepanel
		});
		win.show();
	},
	onDeleteMenu : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示", "请选中一个菜单节点！");
			return;
		}
		Ext.MessageBox.confirm("确认框", "确定删除该菜单吗？", function(data) {
			if (data == "yes") {
				var id = selectNode.id;
				Ext.getBody().mask("正在删除，请稍候....");
				Ext.Ajax.request({
					url : ExtJ.getActionPath("/cud/deleteMenu.do"),
					params : {
						id : id
					},
					callback : function(options, suc, response) {
						if (suc) {
							Ext.Msg.alert('提示', '删除成功！');
						} else {
							Ext.Msg.alert('提示', '删除失败！');
						}
						treepanel.reload();
						parent.Ext.getCmp('mainMenu').getRootNode().reload();
						Ext.getBody().unmask(true);
					}.createDelegate(this)
				});
			} else {
				return;
			}
		});
	},
	onUpdateMenu : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		var treepanel = this.westPanel.getRootNode();
		if (!selectNode) {
			Ext.Msg.alert("提示", "请选中一个菜单节点！");
			return;
		}
		var win = new admin.menu.MenuUpdate({
			selectedTreeNode : selectNode,
			treepanel : treepanel
		});
		win.show();
	}
});